package com.foreknow.controller;

import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class ManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String method = req.getParameter("q"); //q=list
        if("list".equals(method)) {
            list(req,resp);
        }else if("delete".equals(method)) {
            delete(req,resp);
        }else if("update".equals(method)) {
            update(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器向客户端响应的内容的类型（MIME）
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端请求的字符编码集为utf-8
        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        GuestbookService service = new GuestbookServiceImpl();
        //获取请求参数
        String id = req.getParameter("id");
        boolean isRight = service.deleteById(Integer.parseInt(id));
        if(isRight) {
            req.getRequestDispatcher("manager?q=list").forward(req,resp);
        }
    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器向客户端响应的内容的类型（MIME）
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端请求的字符编码集为utf-8
        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        GuestbookService service = new GuestbookServiceImpl();
        List<Object> list = service.queryInfo();
        //将list集合在服务器端保存
        session.setAttribute("list",list); //保存在session中的数据或以被多个页面共享
        //将资源跳转到list.jsp，主要是为了在jsp页面中显示查询到的数据
        //req.getRequestDispatcher("/list.jsp").forward(req,resp);
        resp.sendRedirect("/ManagerSystemWebProject/list.jsp");
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器向客户端响应的内容的类型（MIME）
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端请求的字符编码集为utf-8
        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        //调用service的方法
        GuestbookService service = new GuestbookServiceImpl();
        Guestbook gb = service.getQueryById(Integer.parseInt(req.getParameter("id")));
        req.setAttribute("gb",gb);
        //转发请求
        req.getRequestDispatcher("/update.jsp").forward(req,resp);
    }
}
